package algorithm.map;

import java.util.ArrayList;

/**
 * @author bingo
 * @Description 基于链表实现的Map
 * @Date 2018/7/9
 */
public class LinkListMap<K,V> implements Map<K,V> {

    class Node{

        private K key;

        private V value;

        private Node next;

        public Node(K key, V value, Node next) {
            this.key = key;
            this.value = value;
            this.next = next;
        }

        public Node() {
          this(null,null,null);
        }

        @Override
        public String toString() {
            return "Node{" +
                    "key=" + key +
                    ", value=" + value +
                    '}';
        }
    }

    public LinkListMap() {

       dumyHead = new Node();
    }

    private Node dumyHead;

    private Integer size = 0;



    public Node getNode(K key){
        Node resNode = dumyHead.next;
        while (resNode!=null){
            if(resNode.key.equals(key)){
                return  resNode;
            }
            resNode = resNode.next;
        }
        return resNode;
    }

    @Override
    public void add(K key,V value) {

        Node resNode = getNode(key);
        if(resNode==null){
             dumyHead.next = new Node(key,value,dumyHead.next);
             size++;
        }
        else{

            resNode.value = value;
        }



    }

    @Override
    public void set(K key, V value) {
        Node node =  getNode(key);
        if(node==null){
            throw  new IllegalArgumentException("can't find the key named "+key);
        }

        node.value = value;


    }

    @Override
    public V remove(K key) {

        Node prevNode = dumyHead;
        while (prevNode.next!=null){
         if(prevNode.next.key.equals(key)){
             break;
         }
            prevNode  = prevNode.next;
        }

        Node deleteNode = prevNode.next;
        prevNode.next = deleteNode.next;
        deleteNode.next = null;
        size--;
        return deleteNode.value;
    }

    @Override
    public V get(K key) {
        return getNode(key).value;
    }

    @Override
    public boolean contains(K key) {

        return  getNode(key) == null ? false : true;

    }

    public Integer getSize(){
        return  size;
    }

    public static void main(String[] args){

        System.out.println("Pride and Prejudice");

        ArrayList<String> words = new ArrayList<>();
        if(FileOperation.readFile("demo/pride-and-prejudice.txt", words)) {
            System.out.println("Total words: " + words.size());

            LinkListMap<String, Integer> map = new LinkListMap<>();
            for (String word : words) {
                if (map.contains(word))
                    map.set(word, map.get(word) + 1);
                else
                    map.add(word, 1);
            }

            System.out.println("Total different words: " + map.getSize());
            System.out.println("Frequency of PRIDE: " + map.get("pride"));
            System.out.println("Frequency of PREJUDICE: " + map.get("prejudice"));
        }

        System.out.println();
    }
}
